package logic

import (
	"context"
	"errors"
	"github.com/zeromicro/go-zero/core/logx"
	"goods-srv/goods"
	"goods-srv/internal/svc"
	"goods-srv/model/mysql"
	"goods-srv/model/redis"
)

type ReleaseGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewReleaseGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ReleaseGoodsLogic {
	return &ReleaseGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ReleaseGoodsLogic) ReleaseGoods(in *goods.ReleaseGoodsRequest) (*goods.ReleaseGoodsResponse, error) {
	// todo: add your logic here and delete this line
	g := mysql.Goods{
		GoodsTitle:  in.GoodsTitle,
		GoodsPrice:  in.GoodsPrice,
		GoodsStock:  in.GoodsStock,
		GoodsCover:  in.GoodsCover,
		GoodsStatus: in.GoodsStatus,
		GoodsType:   in.GoodsType,
	}
	//u := mysql.User{
	//	Model: gorm.Model{ID: uint(in.UserId)},
	//}
	//err := u.FindUserId()
	//if err != nil {
	//	return nil, errors.New("查询失败")
	//}
	err := g.CreateGoods()
	if err != nil {
		return nil, errors.New("发布商品失败")
	}
	err = redis.AddGoodsStock(int64(g.ID), g.GoodsStock)
	if err != nil {
		return nil, errors.New("虚拟库存添加失败")
	}
	return &goods.ReleaseGoodsResponse{Success: true}, nil
}
